<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
	if(isset($_POST['userid_friend']))
	{
		$user_id = $_COOKIE['studialuserid'];
		$friend_id = $_POST['userid_friend'];
		
		@$connect = mysql_pconnect('localhost', 'root', '');
		if(!$connect)
		{
			echo "Không thể kết nối đến cơ sở dữ liệu, Bạn vui lòng thử lại sau";
			exit;	
		}
		mysql_select_db('studial');
		$query = "select * from `friends` where `user_id` = '$user_id' AND `friend_id` = '$friend_id';";
		$result = mysql_query($query);
		$check_1 = mysql_num_rows($result);
		$query = "select * from `friends` where `user_id` = '$friend_id' AND `friend_id` = '$user_id';";
		$result = mysql_query($query);
		$check_2 = mysql_num_rows($query);
		
		if($check_1 != 0 || $check_2 != 0)
		{
			$query = "insert into `friends` (`id`, `user_id`, `friend_id`) values (NULL, '".$user_id."', '".$friend_id."');";
			mysql_query($query);	
		}
		
	}
?>
</body>
</html>